package com.bgs.travelapp.ticket.service.impl;

import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import com.bgs.travelapp.scenic.entity.ScenicEntity;
import com.bgs.travelapp.scenic.mapper.ScenicMapper;
import com.bgs.travelapp.ticket.entity.TicketEntity;
import com.bgs.travelapp.ticket.entity.sh_areaEntity;
import com.bgs.travelapp.ticket.mapper.TicketMapper;
import com.bgs.travelapp.ticket.mapper.sh_areaMapper;
import com.bgs.travelapp.ticket.service.TicketService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 门票管理 （后端）
 * gpd
 * 2022-07-15
 */
@Service
public class TicketServiceImpl implements TicketService {

    @Resource
    TicketMapper ticketMapper;

    @Resource
    sh_areaMapper sh_areaMapper;

    @Resource
    ScenicMapper scenicMapper;


    /**
     * 新增门票--三级联动（省份查询）（后端---门票管理）
     * @param pid
     * 查询pid为=0的所有省份
     * gpd 2022-07-15 08:30
     * @return
     */
    @Override
    public List<sh_areaEntity> getProvinceAllList(Integer pid) {
        sh_areaEntity sh_areaEntity = new sh_areaEntity();
        sh_areaEntity.setPid(pid);
        return sh_areaMapper.select(sh_areaEntity);
    }



    /**
     * 新增门票--三级联动（城市查询） （后端---门票管理）
     * @param provinceId
     * 查询provinceId为点击省份的所有城市
     * gpd 2022-07-15 08:40
     * @return
     */
    @Override
    public List<sh_areaEntity> getCityAllList(Integer provinceId) {
        sh_areaEntity sh_areaEntity = new sh_areaEntity();
        sh_areaEntity.setPid(provinceId);
        return sh_areaMapper.select(sh_areaEntity);
    }



    /**
     * 新增门票--三级联动（景区查询） （后端---门票管理）
     * @param cityId
     * 查询cityId为点击城市的所有景区
     * gpd 2022-07-15 08:50
     * @return
     */
    @Override
    public List<ScenicEntity> getScenicAllList(Integer cityId) {
        ScenicEntity scenicEntity = new ScenicEntity();
        scenicEntity.setCityId(cityId);
        return scenicMapper.select(scenicEntity);
    }



    /**
     * 新增门票 （后端---门票管理）
     * @param ticketEntity
     * gpd 2022-07-15 09:22
     * @return
     */
    @Override
    public Integer saveTicket(TicketEntity ticketEntity) {
        ticketEntity.setDelFlag(0);
        ticketEntity.setSurplusTickets(ticketEntity.getTickets());
        ticketEntity.setTicketsSold(0);
        return ticketMapper.insertSelective(ticketEntity);
    }


    /**
     * 新增门票--查询景区是否重复录入 （后端---门票管理）
     * @param pid
     * 根据pid查询对应数据库中是否有录入的景区
     * gpd 2022-07-15 09:12
     * @return
     */
    @Override
    public List<TicketEntity> getScenicName(Integer pid) {
        TicketEntity ticketEntity = new TicketEntity();
        ticketEntity.setScenicId(pid);
        return ticketMapper.select(ticketEntity);
    }

    /**
     * 门票管理分页全查+模糊查（景区名称、景区票价）（后端---门票管理）
     * ticketPrice最低票价
     * ticketPrice1最高票价
     * gpd 2022-07-15 09:32
     * @param pageRequest
     * @param scenicName
     * @return
     */
    @Override
    public PageResult getTicketByPage(PageRequest pageRequest, String scenicName,Integer ticketPrice,Integer ticketPrice1) {
        if(ticketPrice==null)
        {
            ticketPrice=0;
        }
        if(ticketPrice1==null){
            ticketPrice1=ticketPrice+99999;
        }
        MyPageHelper.startPage(pageRequest);
        Example example =new Example(TicketEntity.class);
        example.createCriteria().andLike("scenicName","%"+scenicName+"%").andEqualTo("delFlag",0).andBetween("ticketPrice",ticketPrice,ticketPrice1);
        PageResult pageResult =MyPageHelper.pageInfo(ticketMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * 修改门票 （后端---门票管理）
     * gpd 2022-07-15 09:42
     * @param ticketEntity
     * @return
     */
    @Override
    public Integer updateTicket(TicketEntity ticketEntity) {
        if(ticketEntity.getSurplusTickets()+ticketEntity.getTicketsSold()<=ticketEntity.getTickets()){
             return ticketMapper.updateByPrimaryKeySelective(ticketEntity);
        }
        if(ticketEntity.getSurplusTickets()+ticketEntity.getTicketsSold()>ticketEntity.getTickets()){
            ticketEntity.setTickets(ticketEntity.getTickets()+ticketEntity.getSurplusTickets()+ticketEntity.getTicketsSold()-ticketEntity.getTickets());
           return ticketMapper.updateByPrimaryKeySelective(ticketEntity);
        }
        return 1;
    }

    /**
     * 删除门票 （后端---门票管理）
     * @param id
     * 根据id进行删除
     * gpd 2022-07-15 09:52
     * @return
     */
    @Override
    public Integer deleteTicket(Integer id) {
        TicketEntity ticketEntity = new TicketEntity();
        ticketEntity.setId(id);
        ticketEntity.setDelFlag(1);
        return ticketMapper.updateByPrimaryKeySelective(ticketEntity);
    }

    /**
     * 查询景区剩余门票 （后端---门票管理）
     * @param scenicId
     * gpd 2022-07-15 10:22
     * @return
     */
    @Override
    public List<TicketEntity> getSurplusTicket(Integer scenicId) {
        TicketEntity ticketEntity = new TicketEntity();
        ticketEntity.setScenicId(scenicId);
        return ticketMapper.select(ticketEntity);
    }
}
